<?php
include("../dynamicContent.php");
conexionBaseDatos();

if (isset($_GET["tipo"]) && isset($_GET["red"])){
$tipo = $_GET["tipo"];
$red = $_GET["red"];

if ($tipo == 1){//Data
echo $_FILES["file"]["type"];
if ($_FILES["file"]["name"] == ""){
		  $sql = "Insert into cat_Dato (FKID_Categoria,FKID_Idioma,FKID_Ubicacion,nombreDato,descripcionDato,imagenDato,fechaCreacion,fechaUltimaModificacion,estadoDato)  
	  values ('{$_POST['categoria']}','{$_POST['idioma']}','{$_POST['ubicacion']}','{$_POST['titulo']}','{$_POST['descripcion']}','',NOW(),NOW(),'0')";
	  $resultado = mysql_query($sql);  
	  
	  //Modificar fecha de Ubicacion
	    $sql = "update cat_Ubicacion set fechaUltimaActualizacion = NOW() where ID_Ubicacion = '{$_POST['ubicacion']}'";
	  $resultado = mysql_query($sql);	  
	echo "<script type='text/javascript' >alert('Data inserted succesfully');</script>";	  
	  echo "<script type='text/javascript' >window.location='$red';</script>";
	
}


else if ((  ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png")  || ($_FILES["file"]["type"] == "image/bmp"))
&& ($_FILES["file"]["size"] < 20000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
		  echo "<script type='text/javascript' >alert('Error');</script>";
	  echo "<script type='text/javascript' >window.location='$red';</script>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("../images/data/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
	  echo "<script type='text/javascript' >alert('Image alredy exists.');</script>";
	  echo "<script type='text/javascript' >window.location='$red';</script>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/data/" . $_FILES["file"]["name"]);
	  $link = $_FILES["file"]["name"];		
	  $sql = "Insert into cat_Dato (FKID_Categoria,FKID_Idioma,FKID_Ubicacion,nombreDato,descripcionDato,imagenDato,fechaCreacion,fechaUltimaModificacion,estadoDato)  
	  values ('{$_POST['categoria']}','{$_POST['idioma']}','{$_POST['ubicacion']}','{$_POST['titulo']}','{$_POST['descripcion']}','{$link}',NOW(),NOW(),'0')";
	  $resultado = mysql_query($sql);	  
	  
	  //Modificar fecha de Ubicacion
	    $sql = "update cat_Ubicacion set fechaUltimaActualizacion = NOW() where ID_Ubicacion = '{$_POST['ubicacion']}'";
	  $resultado = mysql_query($sql);	  
	echo "<script type='text/javascript' >alert('Data inserted succesfully');</script>";	  
	  echo "<script type='text/javascript' >window.location='$red';</script>";
	  	  
     }
    }
  }
else
  {
  echo "Invalid file";
echo "<script type='text/javascript' >alert('Invalid Image');</script>";
echo "<script type='text/javascript' >window.location='$red';</script>";

}
}

else if ($tipo == 2){//Phrases
$sql = "Insert into frase (FKID_CategoriaFrase, tituloFrase, contenidoFrase, fechaFrase, fechaUltimaActualizacion, estadoFrase)  
	  values ('{$_POST['categoria']}','{$_POST['titulo']}','{$_POST['descripcion']}',NOW(),NOW(),'0')";
	  $resultado = mysql_query($sql);	
echo "<script type='text/javascript' >alert('Phrase inserted succesfully');</script>";	  
	  echo "<script type='text/javascript' >window.location='$red';</script>";
}

else if ($tipo == 3){//Abstracts
echo $_FILES["file"]["type"];

if ((  ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png")  || ($_FILES["file"]["type"] == "image/bmp"))
&& ($_FILES["file"]["size"] < 20000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
		  echo "<script type='text/javascript' >alert('Error');</script>";
	  echo "<script type='text/javascript' >window.location='$red';</script>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("../images/abstracts/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
	  echo "<script type='text/javascript' >alert('Image alredy exists.');</script>";
	  echo "<script type='text/javascript' >window.location='$red';</script>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/abstracts/" . $_FILES["file"]["name"]);
	  $link = $_FILES["file"]["name"];	
	  $sql = "Insert into abstracto (nombreAbstracto,imagenAbstracto,fechaCreacion,UltimaActualizacion,estadoAbstracto)  
	  values ('{$_POST['titulo']}','{$link}',NOW(),NOW(),'0')";
	  $resultado = mysql_query($sql);	  
	   
	echo "<script type='text/javascript' >alert('Abstract inserted succesfully');</script>";	  
	  echo "<script type='text/javascript' >window.location='$red';</script>";
	  	  
     }
    }
  }
else
  {
  echo "Invalid file";
echo "<script type='text/javascript' >alert('Invalid Image');</script>";
echo "<script type='text/javascript' >window.location='$red';</script>";

}

}
















}





?>